<template>
	<view class="container">
		<view class="product-content">
			<view class="li" v-for="(item,index) in list" :key="index" @click="productClick(item)">
				<view class="stock" v-if="item.minPriceSpec.stock == 0">
					<image class="stock-img" src="/static/icons/stock.png" mode=""></image>
				</view>
				<view :class="['li-main',item.minPriceSpec.stock == 0 ?'mask':''] ">
					<view class="u-flex-center" style="padding: 0 18rpx;box-sizing: border-box;">
						<u-image :src="item.cover_image || ''" width="308rpx" height="280rpx"></u-image>
					</view>
					<view class="li-content">
						<view class="li-name">{{item.name}}</view>
						<view class="li-label u-flex" v-if="item.label.length > 0">
							<view class="label" v-for="(val,k) in item.label" :key="k" v-if="k < 2">{{val}}</view>
							<view class="label-active" v-for="(val,k) in giftActivitiesList" :key="k" v-if="k < 1">
								{{`满${val.full_money}加赠`}}
							</view>
						</view>
						<view class="li-footer u-flex-between u-col-end">
							<view class="footer-left u-flex u-col-end">
								<view class="li-price u-flex u-col-end">
									<span style="font-size: 20rpx;line-height: 25rpx;">￥</span>
									<span style="font-size: 32rpx;">{{item.minPriceSpec.price}}</span>
								</view>
								<view class="original-price u-flex u-col-end">
									<span>￥</span>
									<span>{{item.minPriceSpec.original_price}}</span>
								</view>
							</view>
							<view class="footer-right">
								<view class="source" v-if="item.source_type == 2">跨境商品</view>
								<image v-else :src="$getImageUrl('/user_client/static/icons/add-shopcart.png')" class="add-shopcart-icon"
									@click.stop="item.minPriceSpec.stock > 0?addShop(item):$showToast('抱歉，当前商品补货中')">
								</image>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="loadmore" v-if="loadmore">
			<loadmore :show="total > 0" :status="loadMoreStatus">
			</loadmore>
		</view>
		<empty :mode="emptyMode" :show="total == 0" :text="emptyText" :top="emptyTop" :bottom="emptyBottom">
		</empty>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				isShopCart: true,
			}
		},
		props: {
			list: {
				type: Array,
				defult: []
			},
			total: {
				type: Number,
				default: 0
			},
			label: {
				type: Boolean,
				default: false
			},
			loadmore: {
				type: Boolean,
				default: true
			},
			loadMoreStatus: {
				type: Number,
				default: 1
			},
			emptyText: {
				type: String,
				default: '暂无商品'
			},
			emptyMode: {
				type: String,
				default: 'full-screen'
			},
			emptyTop: {
				type: String,
				default: '100rpx'
			},
			emptyBottom: {
				type: String,
				default: '100rpx'
			},
			type: {
				type: [String, Number],
				default: 1
			}
		},
		// watch: {
		// 	list(newVal, oldVal) {
		// 		console.log(newVal)
		// 		console.log(oldVal)
		// 	}
		// },
		computed: {
			...mapState({
				giftActivitiesList: state => state.init.giftActivitiesList,
			}),
		},
		methods: {
			// 添加购物车
			addShop(e) {
				if (!this.$isLogin()) {
					this.$navigateTo()
					return false;
				}
				if (this.isShopCart == false) return;
				this.isShopCart = false;
				this.$post("shopCart/addReduce", {
					product_type: 1, // 普通商品
					type: 1, // 加1
					product_id: e.id, // 商品id
					product_spec_id: e.minPriceSpec.id, // 规格id
				}).then(res => {
					this.isShopCart = true;
					this.$showToast(res.msg);
					if (res.code == 200) {
						// 更新购物车总数
						this.$store.commit('shopCart/setShopCartNumber', res.data.total);
						// 更新已购物车
						this.$store.commit('shopCart/setIsUpdate');
					}
				}).catch(ex => {
					this.isShopCart = true;
					this.$errorToast()
				});
			},
			productClick(item) {
				if (item.source_type == 2) {
					const id = item.yueyang_product_id
				} else {
					if (this.type == 1) {
						this.$emit('productClick', item.id)
					} else {
						this.$navigateTo('/subPackages/product/product-detail?product_id=' + item.id)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.product-content {
			display: grid;
			grid-template-columns: 1fr 1fr;

			.li {
				padding-top: 22rpx;
				border-top: 2rpx solid #ECECEC;
				line-height: 1;
				background-color: #FFFFFF;
				position: relative;

				.li-content {
					width: 347rpx;
					padding: 20rpx 20rpx;
					box-sizing: border-box;

					.li-name {
						font-size: 24rpx;
						color: #333333;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 1;
						overflow: hidden;
					}

					.li-label {
						padding-top: 40rpx;

						.label {
							margin-left: 10rpx;
							border-radius: 2rpx;
							border: 1rpx solid #131313;
							padding: 8rpx;
							box-sizing: border-box;
							font-size: 16rpx;
							color: #131313;
							line-height: 1;
							position: relative;
						}

						.label:first-child {
							margin: 0;
						}

						.label-active {
							margin-left: 10rpx;
							border-radius: 2rpx;
							border: 1rpx solid #131313;
							padding: 8rpx;
							box-sizing: border-box;
							font-size: 16rpx;
							color: #131313;
							line-height: 1;
							position: relative;

							&::after {
								content: 'VIP';
								position: absolute;
								top: -15rpx;
								left: 2rpx;
								background-color: #000000;
								color: #FFFFFF;
								font-size: 14rpx;
								padding: 2rpx;
								box-sizing: border-box;
								line-height: 1;
							}
						}
					}

					.li-footer {
						padding-top: 20rpx;
						line-height: 1;

						.footer-left {
							.li-price {
								color: #CC242B;
							}

							.original-price {
								font-size: 20rpx;
								color: #A7A7A7;
								text-decoration: line-through;
								padding-left: 6rpx;
								line-height: 25rpx;
							}
						}

						.footer-right {
							.source {
								line-height: 1;
								font-size: 20rpx;
								color: #FFFFFF;
								padding: 8rpx 12rpx;
								box-sizing: border-box;
								background-color: #131313;
								border-radius: 1rpx;
							}

							.add-shopcart-icon {
								width: 30rpx;
								height: 30rpx;
							}
						}
					}
				}

				.stock {
					position: absolute;
					top: 0;
					left: -1rpx;
					z-index: 1;

					.stock-img {
						width: 107rpx;
						height: 94rpx;
						display: block;
					}
				}

				.mask {
					background: #FFFFFF;
					opacity: 0.5;
				}
			}

			.li:nth-child(1) {
				border-top-left-radius: 8rpx;
			}

			.li:nth-child(2) {
				border-top-right-radius: 8rpx;
			}

			.li:nth-child(odd) {
				border-right: 1rpx solid #ECECEC;
			}

			.li:nth-child(even) {
				border-left: 1rpx solid #ECECEC;
			}
		}
	}
</style>